import VueRouter from 'vue-router'
import Vue from 'vue'
import auth from '@/utils/auth'
const Login = () => import('@/views/login.vue')
const Layout = () => import('@/views/Layout.vue')
const Home = () => import('@/home/index.vue')
const Article = () => import('@/article/index.vue')
const Publish = () => import('@/publish/index.vue')
Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    {
      path: '/login',
      component: Login
    },
    {
      path: '/',
      component: Layout,
      children: [
        {
          path: '/',
          component: Home
        },
        {
          path: '/article',
          component: Article
        },
        {
          path: '/publish',
          component: Publish
        }
      ]
    }

  ]
})
router.beforeEach((to, from, next) => {
  // 获取token
  const token = auth.getToken()
  // 不是访问登录，有没有token，跳转登录页面
  if (to.path !== '/login' && !token) return next('/login')
  // 其他情况放行
  next()
})

export default router
